#include <iostream>
#include <unistd.h>

#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>
#include <libpmemobj++/container/string.hpp>

struct object {
    pmem::obj::string object_str;
    pmem::obj::p<int> object_int;
    object() {}
    object(const char * str, int num): object_str(str), object_int(num) {};
};

struct compound_type {
    void set_a(int val) {
        a = val;
    }
    int a;
    double b;
};

struct root {
    pmem::obj::p<int> counter;
    pmem::obj::p<compound_type> whoops;
};

pmem::obj::persistent_ptr<root> r;

static int file_exists(char const *file) {
    return access(file, F_OK);
}
void p_property_example(pmem::obj::pool_base &pop)
{
    pmem::obj::transaction::run(pop, [&] {
		r->counter = 12; 
		r->whoops.get_rw().set_a(2);
		r->whoops.get_rw().b = 3.0;
	});
}
void p_property_show(pmem::obj::pool_base &pop)
{
    std::cout << r->counter << "--" << r->whoops.get_rw().a << "--" << r->whoops.get_rw().b << std::endl;
}
int main() {
	pmem::obj::pool<root> pop;
    const char * path = "dst_file";
    if (file_exists(path) != 0) {
        pop = pmem::obj::pool<root>::create(path, "string_example", PMEMOBJ_MIN_POOL);
    } else {
        pop = pmem::obj::pool<root>::open(path, "string_example");
    }
    r = pop.root();
    p_property_show(pop);
	p_property_example(pop);
    p_property_show(pop);
	pop.close();
	return 0;
}

// g++ -I/usr/local/include pmemobjcpp_string.cpp -lpmemobj -lpthread 